<?php

date_default_timezone_set(DB_TIMEZONE);

class db {
    
    private $link;
    
    private static $single_instance = null;
    
    public static function instance() {
        if (is_null(self::$single_instance)) {
            self::$single_instance = new db();
        }
        return self::$single_instance;
    }
    
    function __construct($host = DB_HOST, $username = DB_USER, $password = DB_PASSWORD, $dbname = DB_NAME) {
        $this->link = mysql_pconnect($host, $username, $password);
        mysql_select_db($dbname, $this->link);
    }
    
    function __destruct() {
        if ($this->link != null) {
            mysql_close($this->link);
            $this->link = null;
        }
    }
    
    function link() {
        return $this->link;
    }
    
    protected function close() {
        if ($this->link != null) {
            mysql_close($this->link);
            $this->link = null;
        }
    }
}

?>